﻿using System;
using App.Entity;
using App.IServices;
using App.IRepository;
using System.Collections.Generic;
using App.Code;

/*!
* 文件描述：一切从简，只为了更懒！
*/
namespace App.Services
{
	public partial class Sys_RoleServices : BaseServices<Sys_Role> , ISys_RoleServices
	{
		ISys_RoleRepository _ISys_RoleRepository;
        ISys_RoleAuthorizeRepository _ISys_RoleAuthorizeRepository;
        ISys_ModuleRepository _ISys_ModuleRepository;
        ISys_ModuleButtonRepository _ISys_ModuleButtonRepository;
        public Sys_RoleServices(ISys_RoleRepository __ISys_RoleRepository, ISys_RoleAuthorizeRepository __ISys_RoleAuthorizeRepository, ISys_ModuleRepository __ISys_ModuleRepository, ISys_ModuleButtonRepository __ISys_ModuleButtonRepository)
		{
			this._ISys_RoleRepository = __ISys_RoleRepository;
			base._IBaseRepository = __ISys_RoleRepository;
            _ISys_RoleAuthorizeRepository = __ISys_RoleAuthorizeRepository;
            _ISys_ModuleRepository = __ISys_ModuleRepository;
            _ISys_ModuleButtonRepository = __ISys_ModuleButtonRepository;

        }

        public void DeleteForm(string keyValue)
        {
            _ISys_RoleRepository.Delete(c => c.Id == keyValue);
            _ISys_RoleAuthorizeRepository.Delete(c => c.ObjectId == keyValue);

        }

        public Sys_Role GetForm(string keyValue)
        {
            return _ISys_RoleRepository.QuerySingle(c => c.Id == keyValue);
        }

        public List<Sys_Role> GetList(string keyword = "")
        {
            var expression = ExtLinq.True<Sys_Role>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FullName.Contains(keyword));
                expression = expression.Or(t => t.EnCode.Contains(keyword));
            }
            expression = expression.And(t => t.Category == 1);
            return _ISys_RoleRepository.QueryByWhere(expression, "SortCode");
        }

        public void SubmitForm(Sys_Role roleEntity, string[] permissionIds, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                roleEntity.Modify(keyValue);
            }
            else
            {
                roleEntity.Create();
            }
            var moduledata = _ISys_ModuleRepository.QueryAll();
            var buttondata = _ISys_ModuleButtonRepository.QueryAll();
            List<Sys_RoleAuthorize> roleAuthorizeEntitys = new List<Sys_RoleAuthorize>();
            foreach (var itemId in permissionIds)
            {
                Sys_RoleAuthorize roleAuthorizeEntity = new Sys_RoleAuthorize();
                roleAuthorizeEntity.Id = Common.GuId();
                roleAuthorizeEntity.ObjectType = 1;
                roleAuthorizeEntity.ObjectId = roleEntity.Id;
                roleAuthorizeEntity.ItemId = itemId;
                if (moduledata.Find(t => t.Id == itemId) != null)
                {
                    roleAuthorizeEntity.ItemType = 1;
                }
                if (buttondata.Find(t => t.Id == itemId) != null)
                {
                    roleAuthorizeEntity.ItemType = 2;
                }
                roleAuthorizeEntitys.Add(roleAuthorizeEntity);
            }

            if (!string.IsNullOrEmpty(keyValue))
            {
             var isbool =   _ISys_RoleRepository.Update(roleEntity);
            }
            else {
                roleEntity.Category = 1;
                var isbool = _ISys_RoleRepository.Insert(roleEntity);
            }
            _ISys_RoleAuthorizeRepository.Delete(t => t.ObjectId == roleEntity.Id);
           var isbools =  _ISys_RoleAuthorizeRepository.InsertRange(roleAuthorizeEntitys);
        }
    }
}